<?php
class News_model extends MY_Model {
	function __construct() {
		parent::__construct('c_news');
	}
	
	/**
     * 公用查询程序
     * $condition['id'] = ?  //按照新闻id搜索
     * $condition['category'] = ?  //按照新闻发布者类别（社团、社联员工）搜索
     * $condition['author_id'] = ?  //按照作者编号（外键:用户id）搜索
     * $condition['is_published'] = ?  //按是否发布搜索  1:未发布 2:已发布
     * $condition['sort_create_time'] = ?  //按照新闻发布时间排序
     * $condition['sort_update_time'] = ?  //按照新闻更新时间排序
     * $condition['sort_view_count'] = ?  //按新闻浏览次数排序
     * $condition['offset'] = ?
     * $condition['per_page'] = ?
     * @param $condition
     * @param $row_str_FLAG 仅对查询结果是单条的：若设为TRUE：单条自动转为字符串， 若设为FALSE：单条自动转为数组
     * @param $count_FLAG 若设为TRUE：计算查询结果数， 若设为FALSE：直接查询结果
     * @param $select_key 留空为查询全部键，否则查询指定键
     */
    function search_by_condition($condition, $row_str_FLAG = FALSE, $count_FLAG = FALSE, $select_key = '') {
		if ($select_key != '') {
			$this->db->select($select_key);
		}

    	if (isset($condition['id'])) {
            $this->db->where('id', $condition['id']);
        }
        
        if (isset($condition['category'])) {
            $this->db->where('category_id', $condition['category']);
        }

        if (isset($condition['author_id'])) {
            $this->db->where('author_id', $condition['author_id']);
        }
        
    	if (isset($condition['is_published'])) {
            $this->db->where('is_published', $condition['is_published']);
        }

        if (!$count_FLAG) {
            if (isset($condition['sort_create_time'])) {
                $this->db->order_by("create_time", $condition['sort_create_time']);
            }
            
        	if (isset($condition['sort_update_time'])) {
                $this->db->order_by("update_time", $condition['sort_update_time']);
            }
            
        	if (isset($condition['sort_view_count'])) {
    			$this->db->order_by("view_count", $condition['sort_view_count']);
    		}

            if (isset($condition['offset'])) {
                if (isset($condition['per_page'])) {
                    $this->db->limit($condition['per_page'], $condition['offset']);
                } else {
                    $this->db->limit(PER_PAGE, $condition['offset']);
                }
            }

            $query = $this->db->get('c_news');
        	if ($query->num_rows() == 1) {
				if (!$row_str_FLAG) {
	            	return $query->result_array();
				} else {
					return $query->row_array();
            	}
			} elseif ($query->num_rows() > 1) {
				return $query->result_array();
			} else {
				return NULL;
			}
        } else {
            $this->db->from('c_news');
            return $this->db->count_all_results();
        }
    }
	
	/**
	 * 得到c_news表的所有信息
	 */
	function get_all_news($offset = 0, $per_page = 10) {
		$sql = "SELECT * FROM c_news ORDER BY top_num DESC limit ?, ?;";
		$query = $this->db->query($sql, array($offset, $per_page));
		return $query->result_array();
	}
	
	function get_author_by_news_id($news_id) {
		$sql = "SELECT author_id FROM c_news WHERE id = ?;";
		$query = $this->db->query($sql, array($news_id));
		$data = $query->row_array();
		return $data['author_id'];
	}
	
	function update_view_count($id) {
		$sql = "UPDATE c_news SET view_count = view_count + 1 WHERE id = ?";
		$this->db->query($sql, array($id));
	}
	
	
}